package com.womeyo.models;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.womeyo.shared.*;;

public class User {

	public String Id;
	public String Username;
	public String Name;
	public String LastName;
	public String Profiles;
	
	public boolean IsRegistered;
	
	public String getFullName()
	{
		String ret = "";
		
		if (Name == null && LastName == null)
		{
			ret = Username;
		}
		else
		{
			ret = String.format("%1$s %2$s", Name, LastName);
		}
		
		return ret;
	}
	
	public static void createOrUpdateUser(String username, String phoneNumber) {
		Entity user = getUser(username);
		if (user == null) {
			user = new Entity("User", username);
			user.setProperty("phoneNumber", phoneNumber);
		} else {
			user.setProperty("phoneNumber", phoneNumber);
		}
		Util.persistEntity(user);
	}

	public static Iterable<Entity> getAllUsers(String kind) {
		return Util.listEntities(kind, null, null);
	}

	/**
	 * Get product entity
	 * 
	 * @param name
	 *          : name of the product
	 * @return: product entity
	 */
	public static Entity getUser(String username) {
		Key key = KeyFactory.createKey("User", username);
		return Util.findEntity(key);
	}
}